Basic Numeric Fields
Numeric fields are just like text fields but work well with numeric values of any type. The input is automatically restricted to numeric values and it works regardless of the browser locale settings for decimal types.
Min and Max attributes allow to restrict the value within the limits. If they are not specified, the full value range for the type (i.e -2147483648 and 2147483647 for int) is permitted.
The Step attribute defines how much the value changes when using the up/down buttons on the right, or with the up/down keys on the keyboard. If not specified, the default value is 1.
<MudNumericField @bind-Value="IntValue" Label="Standard" Variant="Variant.Text" Min="0" Max="10" /> <MudNumericField @bind-Value="DoubleValue" Label="Filled" Variant="Variant.Filled" Min="0.0" /> <MudNumericField @bind-Value="DecimalValue" Label="Outlined" Variant="Variant.Outlined" Step=".2M" />
@code { public int IntValue { get; set; } public double DoubleValue { get; set; } public decimal DecimalValue { get; set; } }
<MudNumericField HideSpinButtons="true" @bind-Value="IntValue" Label="Standard" Variant="Variant.Text" Min="0" Max="10" /> <MudNumericField HideSpinButtons="true" @bind-Value="DoubleValue" Label="Filled" Variant="Variant.Filled" Min="0.0" /> <MudNumericField HideSpinButtons="true" @bind-Value="DecimalValue" Label="Outlined" Variant="Variant.Outlined" Step=".2M" />
@code { public int IntValue { get; set; } public double DoubleValue { get; set; } public decimal DecimalValue { get; set; } }
Setting Culture and Format
You can set a custom culture and format which will be used for parsing the numeric value from the input string as well as for presentation.
@using System.Globalization <MudNumericField Immediate="false" Label="de-DE" Format="N2" Culture="@_de" T="double?" @bind-Value="_valueDe" HelperText="@_valueDe.ToString()"/> <MudNumericField Immediate="false" Label="en-US" Format="N2" Culture="@_en" T="double?" @bind-Value="_valueEn" HelperText="@_valueEn.ToString()"/>
@code { public CultureInfo _de = CultureInfo.GetCultureInfo("de-DE"); public CultureInfo _en = CultureInfo.GetCultureInfo("en-US"); public double? _valueDe = 1234.56; public double? _valueEn = 1234.56; }
Binding Value Types vs Nullables
When you bind value types, the numeric field will not be empty even if the user hasn't entered a value yet because a value type always has a value, even when unassigned.
The two-way-bindable Value property will automatically assume the default value of that type (i.e. 0 for int).
Thus, if you want your numeric fields to be empty, as long as nothing has been entered yet or after the field has been cleared, use the nullable version of that type (i.e. int?).
Do not assign null values to Min, Max and Step.
<MudNumericField @bind-Value="intValue" Label="Enter an int" Variant="Variant.Outlined" /> <MudNumericField @bind-Value="doubleValue" Label="Enter a double" Format="F1" Variant="Variant.Outlined" /> <MudNumericField @bind-Value="nullableInt" Label="Enter an int" Variant="Variant.Outlined" /> <MudNumericField @bind-Value="nullableDouble" Label="Enter a double" Format="F1" Variant="Variant.Outlined" />
@code { int intValue; double doubleValue; int? nullableInt; double? nullableDouble; }